package com.ziilion;





import android.content.Context;  
import android.graphics.Rect;  
import android.text.TextPaint;  
import android.util.AttributeSet;
import android.view.animation.LinearInterpolator;  
import android.widget.Scroller;  
import android.widget.TextView;  
public class ScrollTextView extends TextView {  

// scrolling feature  
private Scroller mSlr;  

// milliseconds for a round of scrolling  

private int mRndDuration = 250;  

// the X offset when paused  

private int mXPaused = 0;  

// whether it's being paused  

private boolean mPaused = true;  

 

public ScrollTextView(Context context) {  
this(context, null);  
}  

public ScrollTextView(Context context, AttributeSet attrs) {  

this(context, attrs, android.R.attr.textViewStyle);  

}  


public ScrollTextView(Context context, AttributeSet attrs, int defStyle) {  

super(context, attrs, defStyle);  



setSingleLine();  

setEllipsize(null);  

setVisibility(INVISIBLE);  

}  


public void startScroll() {  

mXPaused = -1 * getWidth();  

mPaused = true;  

resumeScroll();  

}  


public void resumeScroll() {  

if (!mPaused)  

return;  

setHorizontallyScrolling(true);  

mSlr = new Scroller(this.getContext(), new LinearInterpolator());  

setScroller(mSlr);  
int scrollingLen = calculateScrollingLen();  

int distance = scrollingLen - (getWidth() + mXPaused);  

int duration = (new Double(mRndDuration * distance * 1.00000 / scrollingLen)).intValue();  

setVisibility(VISIBLE);  

mSlr.startScroll(mXPaused, 0, distance, 0, duration);  

mPaused = false;  

}  


private int calculateScrollingLen() {  

TextPaint tp = getPaint();  

Rect rect = new Rect();  

String strTxt = getText().toString();  

tp.getTextBounds(strTxt, 0, strTxt.length(), rect);  

int scrollingLen = rect.width() + getWidth();  

rect = null;  

return scrollingLen;  

}  


public void pauseScroll() {
if (null == mSlr)
return;

if (mPaused)
return;

mPaused = true;

// abortAnimation sets the current X to be the final X,
// and sets isFinished to be true
// so current position shall be saved
mXPaused = mSlr.getCurrX();

mSlr.abortAnimation();
}

@Override
/*
* override the computeScroll to restart scrolling when finished so as that
* the text is scrolled forever
*/
public void computeScroll() {
super.computeScroll();

if (null == mSlr) return;

if (mSlr.isFinished() && (!mPaused)) {
this.startScroll();
}
}

public int getRndDuration() {
return mRndDuration;
}

public void setRndDuration(int duration) {
this.mRndDuration = duration;
}

public boolean isPaused() {
return mPaused;
}
}
